package com.swu.pad.system.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swu.pad.system.dto.ResultDto;
import com.swu.pad.system.entity.Dictionary;
import com.swu.pad.system.entity.User;
import com.swu.pad.system.service.DictionaryService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	@Autowired
	private DictionaryService dictionaryService;

	/**
	 * 404，请求方法不存在
	 * @return
	 */
	@RequestMapping(value = "*")
	public ResultDto noFunction() {
		ResultDto result = new ResultDto();
		result.setCode(ResultDto.CODE_NO_SUCH_FUNCTION);
		result.setMessage("请求的方法不存在！");
		result.setData(new HashMap<String, Object>());
		return result;
	}

	@RequestMapping(value = "/")
	public String home(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (User.ROLE_STUDENT.equals(user.getRole())) {
			return "student";
		} else {
			return "teacher";
		}
	}

	@RequestMapping(value = "/edit")
	public String edit(String assignmentId, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		User user = (User) session.getAttribute("user");
		if (User.ROLE_STUDENT.equals(user.getRole())) {
			return "edit";
		} else {
			return "teacher";
		}
	}

	@ResponseBody
	@RequestMapping(value = "/addDictionary")
	public String addDictionary(Dictionary dictionary) {
		dictionaryService.addDictionary(dictionary);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getDictionary")
	public List<Dictionary> getDictionary(String code) {
		return dictionaryService.getDictionary(code);
	}

	@ResponseBody
	@RequestMapping(value = "/deleteDictionary")
	public String deleteDictionary(String dictionaryId) {
		dictionaryService.deleteDictionary(dictionaryId);
		return "success";
	}

}
